package procsim;

import javax.swing.*;
import java.awt.*;
import java.util.*;

public class Paint extends JPanel{
    private JTabbedPane tabbed = new JTabbedPane(JTabbedPane.LEFT);
    private ArrayList<PaintTab> list = new ArrayList<PaintTab>();
    private int index = 0;
    private Program parent;
    public Paint(Program parent){
        this.parent=parent;
        setUp(new PaintCPU(this), "CPU", "Processor");
        // registri
        setUp(new PaintPC(this), "PC", "Program counter");
        setUp(new PaintInstructionFetch(this), "IF", "Instruction fetching");
        setUp(new PaintInstructionDecode(this), "ID", "Instruction decoding");
        setUp(new PaintAuxiliary(this), "Aux", "Generation of auxiliary signals");
        setUp(new PaintRegisters1(this), "Registers 1", "Registers");
        setUp(new PaintRegisters2(this), "Registers 2", "Registers");
        setUp(new PaintXY(this), "X & Y", "X and Y registers");
        setUp(new PaintDISP(this), "DISP", "Calculation of displacement");
        setUp(new PaintPSW1(this), "PSW 1", "Program status word");
        setUp(new PaintPSW2(this), "PSW 2", "Program status word");
        // operacije
        setUp(new PaintALU(this), "ALU", "Arithmetic logical unit");
        setUp(new PaintIndicators(this), "Indicators", "ALU indicators");
        // prekidi
        setUp(new PaintMaskedInterruptsAck(this), "Masked IRQ ACK", "Masked interrupts acknowledgement");
        setUp(new PaintIMR(this), "IMR", "IMR register and internal interrupts");
        setUp(new PaintMaskedInterrupts(this), "Masked IRQ", "Masked interrupts");
        setUp(new PaintInterrupts(this), "IVTP", "Calculation of the address of the interrupt routine");
        // interfejs
        setUp(new PaintArbitrator(this), "Arbitration", "Arbitrator");
        setUp(new PaintSynchronization(this), "Synchronization", "CPU/Bus synchronization");
        setUp(new PaintMARMDR(this), "MAR/MDR", "MAR and MDR registers");
        // memorija
        setUp(new PaintMEM1(this), "Memory 1", "Memory");
        setUp(new PaintMEM2(this), "Memory 2", "Memory");
        // upravljacka jedinica
        setUp(new PaintControlUnit1(this), "CTRLUnit 1", "Control Unit");
        setUp(new PaintControlUnit2(this), "CTRLUnit 2", "Control Unit");
        setUp(new PaintCUOPR(this), "CU OPR", "Determining the branch address for operations");
        setUp(new PaintCUBRANCH(this), "CU BRANCH", "Determining the branch address");
        setUp(new PaintSignalsOP(this), "CU signals OP", "Generating operational signals of Control Unit");
        setUp(new PaintSignalsCU(this), "CU signals CU", "Generating control signals of Control Unit");
        tabbed.setSelectedIndex(0);
        tabbed.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
        setLayout(new GridLayout());
        add(tabbed);
    }

    private void setUp(PaintTab pt, String title, String ttt){
        JPanel pan = new JPanel(new GridLayout());
        pan.add(pt);
        tabbed.addTab("<html><font size=4>" + title + "</font></html>", pan);
        tabbed.setToolTipTextAt(index++, ttt);
        list.add(pt);
    }

    public void rePaint(){
        Iterator iter = list.iterator();
        while(iter.hasNext())
            ((PaintTab)iter.next()).repaint();
    }

    public void setSelectedTab(int index){
        tabbed.setSelectedIndex(index);
    }
}
